#!/bin/bash

# 读取变量
source ./.env

echo "拉取镜像"
# docker pull elasticsearch:$elasticsearch_version
docker load -i elasticsearch-7.17.1.tar

echo "创建目录"
mkdir es-01 es-01/data es-01/plugins es-01/conf

chmod -R 777 es-01

echo "创建配置文件"
touch es-01/conf/elasticsearch.yml
cat << EOF > es-01/conf/elasticsearch.yml
cluster.name: $cluster_name
node.name: $node01_name
network.host: 0.0.0.0
http.port: $node01_inside_port
transport.tcp.port: $node01_inside_tcp_port
node.master: true
discovery.seed_hosts: ["$node01_ip:$node01_outside_tcp_port","$node02_ip:$node02_outside_tcp_port","$node03_ip:$node03_outside_tcp_port"]
cluster.initial_master_nodes: ["$node01_name","$node02_name","$node03_name"]
EOF

echo "创建启动脚本"
touch es-01/start.sh
cat << EOF > es-01/start.sh
#!/bin/bash
docker run \
    --name $node01_name \
    -v $(pwd)/es-01/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
    -v $(pwd)/es-01/data:/usr/share/elasticsearch/data \
    -v $(pwd)/es-01/plugins:/usr/share/elasticsearch/plugins \
	-e ES_JAVA_OPTS='-Xms$es_java_xms -Xmx$es_java_xmx' \
    -p $node01_outside_port:$node01_inside_port \
    -p $node01_outside_tcp_port:$node01_inside_tcp_port \
    -d docker.io/elasticsearch:$elasticsearch_version
EOF

echo "启动容器"
sh es-01/start.sh